home *** CD-ROM | disk | FTP | other *** search
/ Chip 2007 January, February, March & April / Chip-Cover-CD-2007-02.iso / Pakiet bezpieczenstwa / mini Pentoo LiveCD 2006.1 / mpentoo-2006.1.iso / livecd.squashfs / usr / lib / python2.4 / asynchat.pyo (.txt) < prev    next >
Python Compiled Bytecode  |  2005-10-18  |  8KB  |  240 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyo (Python 2.4)
  3.  
  4. """A class supporting chat-style (command/response) protocols.
  5.  
  6. This class adds support for 'chat' style protocols - where one side
  7. sends a 'command', and the other sends a response (examples would be
  8. the common internet protocols - smtp, nntp, ftp, etc..).
  9.  
  10. The handle_read() method looks at the input stream for the current
  11. 'terminator' (usually '\\r\\n' for single-line responses, '\\r\\n.\\r\\n'
  12. for multi-line output), calling self.found_terminator() on its
  13. receipt.
  14.  
  15. for example:
  16. Say you build an async nntp client using this class.  At the start
  17. of the connection, you'll have self.terminator set to '\\r\\n', in
  18. order to process the single-line greeting.  Just before issuing a
  19. 'LIST' command you'll set it to '\\r\\n.\\r\\n'.  The output of the LIST
  20. command will be accumulated (using your own 'collect_incoming_data'
  21. method) up to the terminator, and then control will be returned to
  22. you - by calling your self.found_terminator() method.
  23. """
  24. import socket
  25. import asyncore
  26. from collections import deque
  27.  
  28. class async_chat(asyncore.dispatcher):
  29.     '''This is an abstract class.  You must derive from this class, and add
  30.     the two methods collect_incoming_data() and found_terminator()'''
  31.     ac_in_buffer_size = 4096
  32.     ac_out_buffer_size = 4096
  33.     
  34.     def __init__(self, conn = None):
  35.         self.ac_in_buffer = ''
  36.         self.ac_out_buffer = ''
  37.         self.producer_fifo = fifo()
  38.         asyncore.dispatcher.__init__(self, conn)
  39.  
  40.     
  41.     def collect_incoming_data(self, data):
  42.         raise NotImplementedError, 'must be implemented in subclass'
  43.  
  44.     
  45.     def found_terminator(self):
  46.         raise NotImplementedError, 'must be implemented in subclass'
  47.  
  48.     
  49.     def set_terminator(self, term):
  50.         '''Set the input delimiter.  Can be a fixed string of any length, an integer, or None'''
  51.         self.terminator = term
  52.  
  53.     
  54.     def get_terminator(self):
  55.         return self.terminator
  56.  
  57.     
  58.     def handle_read(self):
  59.         
  60.         try:
  61.             data = self.recv(self.ac_in_buffer_size)
  62.         except socket.error:
  63.             why = None
  64.             self.handle_error()
  65.             return None
  66.  
  67.         self.ac_in_buffer = self.ac_in_buffer + data
  68.         while self.ac_in_buffer:
  69.             lb = len(self.ac_in_buffer)
  70.             terminator = self.get_terminator()
  71.             None if terminator is None or terminator == '' else lb < n
  72.             terminator_len = len(terminator)
  73.             index = self.ac_in_buffer.find(terminator)
  74.             if index != -1:
  75.                 if index > 0:
  76.                     self.collect_incoming_data(self.ac_in_buffer[:index])
  77.                 
  78.                 self.ac_in_buffer = self.ac_in_buffer[index + terminator_len:]
  79.                 self.found_terminator()
  80.                 continue
  81.             index = find_prefix_at_end(self.ac_in_buffer, terminator)
  82.             if index:
  83.                 if index != lb:
  84.                     self.collect_incoming_data(self.ac_in_buffer[:-index])
  85.                     self.ac_in_buffer = self.ac_in_buffer[-index:]
  86.                 
  87.                 break
  88.                 continue
  89.             self.collect_incoming_data(self.ac_in_buffer)
  90.             self.ac_in_buffer = ''
  91.  
  92.     
  93.     def handle_write(self):
  94.         self.initiate_send()
  95.  
  96.     
  97.     def handle_close(self):
  98.         self.close()
  99.  
  100.     
  101.     def push(self, data):
  102.         self.producer_fifo.push(simple_producer(data))
  103.         self.initiate_send()
  104.  
  105.     
  106.     def push_with_producer(self, producer):
  107.         self.producer_fifo.push(producer)
  108.         self.initiate_send()
  109.  
  110.     
  111.     def readable(self):
  112.         '''predicate for inclusion in the readable for select()'''
  113.         return len(self.ac_in_buffer) <= self.ac_in_buffer_size
  114.  
  115.     
  116.     def writable(self):
  117.         '''predicate for inclusion in the writable for select()'''
  118.         if self.ac_out_buffer == '' and self.producer_fifo.is_empty():
  119.             pass
  120.         return not (self.connected)
  121.  
  122.     
  123.     def close_when_done(self):
  124.         '''automatically close this channel once the outgoing queue is empty'''
  125.         self.producer_fifo.push(None)
  126.  
  127.     
  128.     def refill_buffer(self):
  129.         while len(self.producer_fifo):
  130.             p = self.producer_fifo.first()
  131.             if p is None:
  132.                 if not self.ac_out_buffer:
  133.                     self.producer_fifo.pop()
  134.                     self.close()
  135.                 
  136.                 return None
  137.             elif isinstance(p, str):
  138.                 self.producer_fifo.pop()
  139.                 self.ac_out_buffer = self.ac_out_buffer + p
  140.                 return None
  141.             
  142.             data = p.more()
  143.             if data:
  144.                 self.ac_out_buffer = self.ac_out_buffer + data
  145.                 return None
  146.             else:
  147.                 self.producer_fifo.pop()
  148.             data
  149.             return None
  150.  
  151.     
  152.     def initiate_send(self):
  153.         obs = self.ac_out_buffer_size
  154.         if len(self.ac_out_buffer) < obs:
  155.             self.refill_buffer()
  156.         
  157.         if self.ac_out_buffer and self.connected:
  158.             
  159.             try:
  160.                 num_sent = self.send(self.ac_out_buffer[:obs])
  161.                 if num_sent:
  162.                     self.ac_out_buffer = self.ac_out_buffer[num_sent:]
  163.             except socket.error:
  164.                 why = None
  165.                 self.handle_error()
  166.                 return None
  167.             except:
  168.                 None<EXCEPTION MATCH>socket.error
  169.             
  170.  
  171.         None<EXCEPTION MATCH>socket.error
  172.  
  173.     
  174.     def discard_buffers(self):
  175.         self.ac_in_buffer = ''
  176.         self.ac_out_buffer = ''
  177.         while self.producer_fifo:
  178.             self.producer_fifo.pop()
  179.  
  180.  
  181.  
  182. class simple_producer:
  183.     
  184.     def __init__(self, data, buffer_size = 512):
  185.         self.data = data
  186.         self.buffer_size = buffer_size
  187.  
  188.     
  189.     def more(self):
  190.         if len(self.data) > self.buffer_size:
  191.             result = self.data[:self.buffer_size]
  192.             self.data = self.data[self.buffer_size:]
  193.             return result
  194.         else:
  195.             result = self.data
  196.             self.data = ''
  197.             return result
  198.  
  199.  
  200.  
  201. class fifo:
  202.     
  203.     def __init__(self, list = None):
  204.         if not list:
  205.             self.list = deque()
  206.         else:
  207.             self.list = deque(list)
  208.  
  209.     
  210.     def __len__(self):
  211.         return len(self.list)
  212.  
  213.     
  214.     def is_empty(self):
  215.         return not (self.list)
  216.  
  217.     
  218.     def first(self):
  219.         return self.list[0]
  220.  
  221.     
  222.     def push(self, data):
  223.         self.list.append(data)
  224.  
  225.     
  226.     def pop(self):
  227.         if self.list:
  228.             return (1, self.list.popleft())
  229.         else:
  230.             return (0, None)
  231.  
  232.  
  233.  
  234. def find_prefix_at_end(haystack, needle):
  235.     l = len(needle) - 1
  236.     while l and not haystack.endswith(needle[:l]):
  237.         l -= 1
  238.     return l
  239.  
  240.